package com.hxh.tess4j;

import com.hxh.tess4j.util.DownFileUtil;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;
import java.io.IOException;

/**
 * @author huangxunhui
 * Date: Created in 2019-04-04 14:55
 * Utils: Intellij Idea
 * Description: 识别图像验证码
 */
@Slf4j
public class DiscernCode {

    /**
     * 文件
     */
    private static final String FILE_PATH = "/Users/code/IdeaProjects/tess4j/src/main/resources/img/code_2.jpg";

    static synchronized String code()  {
        try {
//            DownFileUtil.downloadPicture("https://appnew.loserbank.com/dsdreg/yzm/yzm" ,"code.jpg");
            DownFileUtil.downloadPicture("https://qmyiapi.51liandu.com/v1/captcha/create?pictureToken=1&sign=1" ,"code.jpg");

        } catch (IOException e) {
            log.error(e.getMessage());
        }

        File imageFile = new File(FILE_PATH);

        ITesseract instance = new Tesseract();
        instance.setDatapath("/Users/code/IdeaProjects/tess4j/tessdata");
        try {
            return instance.doOCR(imageFile);
        } catch (TesseractException e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(code());
    }
}
